package com.example.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.TrzhkEntity;
import com.example.domain.TrzxmEntity;
import com.example.mapper.TrzMapper;
import com.example.service.TrzService;

@Service
@Transactional
public class TrzServiceImp implements TrzService {
	@Autowired
	private TrzMapper trzMapper;
	/**
	 * 一对多 项目+还款信息
	 */
	@Override
	public List<TrzxmEntity> getTrz() {
		return trzMapper.findAll();
	}

	@Override
	public List<Integer> getYear() {
		List<TrzxmEntity> trzxmEntities = trzMapper.findYear(); //数据库数据
		List<String> yearScope = new ArrayList<String>(); //最大年、最低年
		for (TrzxmEntity trzxmEntity : trzxmEntities) {
			if(trzxmEntity != null) {
				yearScope.add(trzxmEntity.getDqr());
			}
		}
		return fillYears(Integer.parseInt(yearScope.get(yearScope.size() -1)),Integer.parseInt(yearScope.get(0)));	
	}
	/**
	 * 根据【开始年份】和【结束年份】填充中间年份
	 * @param beginYear
	 * @param endYear
	 * @return
	 */
	public List<Integer> fillYears(Integer beginYear, Integer endYear) {
		List<Integer> yearFinal = new ArrayList<Integer>();
		for(int i=1; i<(endYear-beginYear);i++) {
			yearFinal.add(beginYear + i);
		}
		yearFinal.add(beginYear);
		yearFinal.add(endYear);
		Collections.sort(yearFinal);
		return yearFinal;
		
	}
	/**
	 * 还款信息
	 */
	@Override
	public ArrayList<TrzhkEntity> getHKList(String startDate, String endDate) {
		// TODO Auto-generated method stub
		return trzMapper.findHK(startDate, endDate);
	}
	/**
	 * 项目信息
	 */
	@Override
	public ArrayList<TrzxmEntity> getXMList() {
		// TODO Auto-generated method stub
		return trzMapper.findXMList();
	}
	
	
	
}
